﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using System.Globalization;
using System.Collections.ObjectModel;
using bdnt_pets_system.FailureServiceReference;
using bdnt_pets_system.PageViews.FailuresTraking;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.Failures
{
    public partial class SaveFailuresManagerControl : BaseUserControl
    {
        DataServiceReference.DataServiceClient dataClient;
        FailureServiceClient faiClient;
        int pageSize = SessionManager.PageSize;
        int currentPage = 1;
        public Tbl_PartSta_Relation CuPartInfo;

        public SaveFailuresManagerControl(Tbl_PartSta_Relation cuPartInfo)
        {
            InitializeComponent();
            CuPartInfo = cuPartInfo;
            dataClient = new DataServiceReference.DataServiceClient();
            faiClient = new FailureServiceClient();
            InitData();
        }

        void InitData()
        {
            InitEvent();
            QueryData();
        }

        void InitEvent()
        {
            dataClient.GetFailureInfoListByPartNoCompleted += new EventHandler<DataServiceReference.GetFailureInfoListByPartNoCompletedEventArgs>(dataClient_GetFailureInfoListByPartNoCompleted);
            dataClient.GetFailureInfoListByPartNoCountCompleted += new EventHandler<DataServiceReference.GetFailureInfoListByPartNoCountCompletedEventArgs>(dataClient_GetFailureInfoListByPartNoCountCompleted);
            hbPartName.Click += new RoutedEventHandler(hbPartName_Click);
            fauiltPageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPage_PageIndexChanged);
            fauiltRecordPageC.dpPage.PageIndexChanged += new EventHandler<EventArgs>(dpPagefauiltRecord_PageIndexChanged);
            btnBack.Click += new RoutedEventHandler(btnBack_Click);
            btnSaveFauilre.Click += new RoutedEventHandler(btnSaveFauilre_Click);
            btnShowFauilre.Click += new RoutedEventHandler(btnShowFauilre_Click);
            btnNextV.Click += new RoutedEventHandler(btnNextV_Click);

            dataClient.GetNextVehicleByProNoCompleted += new EventHandler<DataServiceReference.GetNextVehicleByProNoCompletedEventArgs>(dataClient_GetNextVehicleByProNoCompleted);
            faiClient.UpdateFailureRecordCompleted += new EventHandler<UpdateFailureRecordCompletedEventArgs>(faiClient_UpdateFailureRecordCompleted);
            faiClient.DeleteFailureRecordCompleted += new EventHandler<DeleteFailureRecordCompletedEventArgs>(faiClient_DeleteFailureRecordCompleted);

            faiClient.NewAndSaveFailureRecordCompleted += new EventHandler<NewAndSaveFailureRecordCompletedEventArgs>(faiClient_NewAndSaveFailureRecordCompleted);
            tbFauilteName.KeyDown += new KeyEventHandler(acFauilteName_KeyDown);
        }
        string failName = ""; 
        void acFauilteName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                failName = tbFauilteName.Text; 
                dataClient.GetFailureInfoListByPartNoCountAsync(CuPartInfo.PartNo, failName, failName);
            }
        }

        void faiClient_NewAndSaveFailureRecordCompleted(object sender, NewAndSaveFailureRecordCompletedEventArgs e)
        {
            var cuPart = (this.Tag as PartSelectControl);
            if (e.Result == -1)
            {
                if (MessageBox.Show("数据库中没有此车辆数据，是否要添加？", "提示", MessageBoxButton.OKCancel) ==
                     MessageBoxResult.Cancel)
                    return;
                NewVehicleWindow nvw = new NewVehicleWindow(new Tbl_Vehicle()
                {
                    VINNo = cuPart.tbVINNo.Text,
                    ProducitonNo = cuPart.tbProductionNo.Text
                });
                nvw.Show();
                nvw.Closed += new EventHandler(nvw_Closed);
            }
            else if (e.Result == -2)
                cuPart.tbErrors.Text = MyStrings.bcsb + "("+MyStrings.yczcfgz+")";//已存在重复故障
            else if (e.Result > 0)
            {
                cuPart.tbErrors.Text = MyStrings.bccg;
                QuerySaveFailures();
            }
            else
                cuPart.tbErrors.Text = MyStrings.bcsb;
        }

        void nvw_Closed(object sender, EventArgs e)
        {
            var cuPart = (this.Tag as PartSelectControl);
            var cuo = sender as NewVehicleWindow;
            if (cuo.DialogResult == true)
            {
                cuPart.CuTbl_FailureRecord.VINNo = cuo.CuTbl_Vehicle.VINNo;
                cuPart.CuTbl_FailureRecord.ProductionNo = cuo.CuTbl_Vehicle.ProducitonNo;
                cuPart.tbCarColorV.Text = cuo.CuTbl_Vehicle.VColor;
                cuPart.tbVConfigurationV.Text = SessionManager.IsCN_ZH ? cuo.CuTbl_Vehicle.VConfigurationName : cuo.CuTbl_Vehicle.en_VConfigurationName;
                SaveMethod();
            }
        }

        void btnNextV_Click(object sender, RoutedEventArgs e)
        {
            var cuPart = (this.Tag as PartSelectControl);
            var cuPro = (cuPart.CuTbl_FailureRecord.ProductionNo == cuPart.selectFailureRecord.ProductionNo ? "0" : cuPart.CuTbl_FailureRecord.ProductionNo);
            long cuP = 0;
            if (string.IsNullOrEmpty(cuPro) || !long.TryParse(cuPro, out cuP))
            {
                return;
            }
            dataClient.GetNextVehicleByProNoAsync(cuP.ToString());
        }

        void dataClient_GetNextVehicleByProNoCompleted(object sender, DataServiceReference.GetNextVehicleByProNoCompletedEventArgs e)
        {
            var cuPart = (this.Tag as PartSelectControl);
            if (e.Result == null)
            {
                cuPart.CuTbl_FailureRecord.ProductionNo = cuPart.selectFailureRecord.ProductionNo;
                cuPart.CuTbl_FailureRecord.VINNo = cuPart.selectFailureRecord.VINNo;
                cuPart.tbVINNo.Foreground = cuPart.tbProductionNo.Foreground = new SolidColorBrush(Colors.Gray);
                cuPart.tbCarColorV.Text = "";
                cuPart.tbVConfigurationV.Text = "";
                cuPart.tbNsColorV.Text = "";
            }
            else
            {
                cuPart.CuTbl_FailureRecord.VINNo = e.Result.VINNo;
                cuPart.CuTbl_FailureRecord.ProductionNo = e.Result.ProducitonNo;
                cuPart.tbCarColorV.Text = e.Result.VColor;
                cuPart.tbVConfigurationV.Text = SessionManager.IsCN_ZH ? e.Result.VConfigurationName : e.Result.en_VConfigurationName;
                cuPart.tbNsColorV.Text = e.Result.InteriorColor;
                cuPart.tbVINNo.Foreground = cuPart.tbProductionNo.Foreground = new SolidColorBrush(Colors.Black);
            }
            QuerySaveFailures();
        }

        void btnShowFauilre_Click(object sender, RoutedEventArgs e)
        {
            var cur = new FailureRecourdManagerControl() { PreBaseUserControl = this.Tag as PartSelectControl };
            cur.btnBack.Visibility = System.Windows.Visibility.Visible;
            App.ToNextPage(cur);
        }

        Tbl_FailureRecord cuSaveFailureRecord;
        void btnSaveFauilre_Click(object sender, RoutedEventArgs e)
        {
            SaveMethod();
        }

        void SaveMethod()
        {
            var cuPart = (this.Tag as PartSelectControl);
            var cuRecord = cuPart.CuTbl_FailureRecord;
            if (selectPositionInfo == null) cuRecord.PositionNo = "";
            else cuRecord.PositionNo = selectPositionInfo.DictValue;
            if (selectFailure.Count < 1) cuRecord.FailureId = 0;
            else cuRecord.FailureId = selectFailure.FirstOrDefault().Item1.Id;
            if (!cuPart.BindgExpression(cuRecord))
                return;
            cuPart.tbErrors.Text = "";
            ObservableCollection<SimpleObject<Int32, Int32>> cuFailures = new ObservableCollection<SimpleObject<int, int>>();
            cuSaveFailureRecord = new Tbl_FailureRecord()
            {
                PartNo = CuPartInfo.PartNo,
                PositionNo = selectPositionInfo.DictValue,
                RecordTime = DateTime.Now,
                RecordUserId = SessionManager.CurrentUser.Id,
                VINNo = cuRecord.VINNo,
                ProductionNo = cuRecord.ProductionNo,
                CheckerUserId = Int32.Parse(cuPart.cbEmpName.SelectedValue.ToString()),
                CheckTime = cuRecord.CheckTime,
                RepairResult = "0",
                FCount = 1
            };
            selectFailure.ForEach(p =>
                cuFailures.Add(new SimpleObject<int, int>()
                {
                    Item1 = p.Item1.Id,
                    Item2 = p.Item2
                }));
            faiClient.NewAndSaveFailureRecordAsync(cuSaveFailureRecord, cuFailures);
            //dataClient.GetVehicleByProNoOrVinNoAsync(cuSaveFailureRecord.ProductionNo, cuSaveFailureRecord.VINNo, cuFailures);

        }

        void btnBack_Click(object sender, RoutedEventArgs e)
        {
            (this.Tag as PartSelectControl).gdParts.Visibility = System.Windows.Visibility.Visible;
            (this.Tag as PartSelectControl).cbEmpName.IsEnabled = (this.Tag as PartSelectControl).cbShopName.IsEnabled = (this.Tag as PartSelectControl).cbStationName.IsEnabled = true;

            this.Visibility = System.Windows.Visibility.Collapsed;
        }

        #region 查询与分页
        public void QueryData()
        {
            hbPartName.Content = SessionManager.IsCN_ZH ? CuPartInfo.PartName + "(" + CuPartInfo.en_PartName + ")" : CuPartInfo.en_PartName;

            #region 查询位置
            SystemServiceReference.SystemServiceClient sysClient = new SystemServiceReference.SystemServiceClient();
            sysClient.GetDictListByPidCompleted += new EventHandler<SystemServiceReference.GetDictListByPidCompletedEventArgs>(sysClient_GetDictListByPidCompleted);
            sysClient.GetDictListByPidAsync(8, 8);
            sysClient.CloseAsync();
            #endregion

            #region 查询返修状态
            SystemServiceReference.SystemServiceClient sysClient1 = new SystemServiceReference.SystemServiceClient();
            sysClient1.GetDictListByPidCompleted += new EventHandler<SystemServiceReference.GetDictListByPidCompletedEventArgs>(sysClient_GetDictListByPidCompleted);
            sysClient1.GetDictListByPidAsync(15, 15);
            sysClient1.CloseAsync();
            #endregion

        }

        void dataClient_GetFailureInfoListByPartNoCountCompleted(object sender, DataServiceReference.GetFailureInfoListByPartNoCountCompletedEventArgs e)
        {
            fauiltPageC.LoadPages(e.Result, pageSize);
        }

        void dpPage_PageIndexChanged(object sender, EventArgs e)
        {
            currentPage = fauiltPageC.PageIndex + 1;
            dataClient.GetFailureInfoListByPartNoAsync(CuPartInfo.PartNo, failName, failName, pageSize, currentPage);
        }

        public List<SimpleObject<Tbl_FailureInfo, int>> selectFailure = new List<SimpleObject<Tbl_FailureInfo, int>>();
        ObservableCollection<Tbl_FailureInfo> failureResult = new ObservableCollection<Tbl_FailureInfo>();
        void dataClient_GetFailureInfoListByPartNoCompleted(object sender, DataServiceReference.GetFailureInfoListByPartNoCompletedEventArgs e)
        {
            failureResult = e.Result;
            ReloadFauilte(); 
            QuerySaveFailures();
        }

        void ReloadFauilte()
        {
          
            if (failureResult == null || failureResult.Count < 1)
                return;
            selectFailure = new List<SimpleObject<Tbl_FailureInfo, int>>();
            wpFailures.Children.Clear();
            foreach (var item in failureResult)
            {
                Border bd = new Border()
                {
                    Margin = new Thickness(0, 0, 10, 10),
                    BorderThickness = new Thickness(0),
                    Width = 150,
                    MinHeight = 100,
                    //Background = new SolidColorBrush(HelpTools.HtmlToColor("#BBBBBB"))
                    Background = new SolidColorBrush(HelpTools.HtmlToColor("#E6EBF1"))
                };
                Grid gd = new Grid();
                gd.RowDefinitions.Add(new RowDefinition());
                gd.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
                gd.ColumnDefinitions.Add(new ColumnDefinition());
                gd.ColumnDefinitions.Add(new ColumnDefinition());
                gd.ColumnDefinitions.Add(new ColumnDefinition());
                bd.Child = gd;
                InitFauiltLevel(item, bd);
                wpFailures.Children.Add(bd);
            }
        }
        
        void InitFauiltLevel(Tbl_FailureInfo item, Border bd)
        {
            Grid gd = bd.Child as Grid;
            gd.Children.Clear();

            TextBlock tb = new TextBlock() { Margin = new Thickness(5) };

            tb.Text = SessionManager.IsCN_ZH ? item.FailureName + "(" + item.en_FailureName + ")" : item.en_FailureName;
            Grid.SetColumnSpan(tb, 3);
            gd.Children.Add(tb);

            Border bdG = new Border() { Tag = bd };
            ToolTipService.SetToolTip(bdG, MyStrings.cqxbtjg + item.FailureGCount.ToString() + MyStrings.ci);
            HyperlinkButton fauilhbG = new HyperlinkButton()
            {
                //BorderBrush = new SolidColorBrush(Colors.LightGray),
                BorderBrush = new SolidColorBrush(HelpTools.HtmlToColor("#EBF1F3")),
                Tag = new SimpleObject<Tbl_FailureInfo, int>() { Item1 = item, Item2 = 2 }
            };
            TextBlock tbG = new TextBlock() { Text = "FC 1" };
            fauilhbG.Content = tbG;
            bdG.Child = fauilhbG;
            Grid.SetRow(bdG, 1);
            gd.Children.Add(bdG);
            fauilhbG.Click += new RoutedEventHandler(fauilhb_Click);
            fauilhbG.MouseEnter += new MouseEventHandler(fauilhbG_MouseEnter);
            fauilhbG.MouseLeave += new MouseEventHandler(fauilhbG_MouseLeave);

            Border bdZ = new Border() { Tag = bd };
            ToolTipService.SetToolTip(bdZ, MyStrings.cqxbtjg + item.FailureZCount.ToString() + MyStrings.ci);
            HyperlinkButton fauilhbZ = new HyperlinkButton()
            {
                //BorderBrush = new SolidColorBrush(Colors.LightGray),
                BorderBrush = new SolidColorBrush(HelpTools.HtmlToColor("#EBF1F3")),
                Tag = new SimpleObject<Tbl_FailureInfo, int>() { Item1 = item, Item2 = 1 }
            };
            TextBlock tbZ = new TextBlock() { Text = "FC 2" };
            fauilhbZ.Content = tbZ;
            bdZ.Child = fauilhbZ;
            Grid.SetRow(bdZ, 1);
            Grid.SetColumn(bdZ, 1);
            gd.Children.Add(bdZ);
            fauilhbZ.Click += new RoutedEventHandler(fauilhb_Click);
            fauilhbZ.MouseEnter += new MouseEventHandler(fauilhbG_MouseEnter);
            fauilhbZ.MouseLeave += new MouseEventHandler(fauilhbG_MouseLeave);

            Border bdD = new Border() { Tag = bd };
            ToolTipService.SetToolTip(bdD, MyStrings.cqxbtjg + item.FailureDCount.ToString() + MyStrings.ci);
            HyperlinkButton fauilhbD = new HyperlinkButton()
            {
                //BorderBrush = new SolidColorBrush(Colors.LightGray),
                BorderBrush = new SolidColorBrush(HelpTools.HtmlToColor("#EBF1F3")),
                Tag = new SimpleObject<Tbl_FailureInfo, int>() { Item1 = item, Item2 = 0 }
            };
            TextBlock tbD = new TextBlock() { Text = "FC 2N" };
            fauilhbD.Content = tbD;
            bdD.Child = fauilhbD;
            Grid.SetRow(bdD, 1);
            Grid.SetColumn(bdD, 2);
            gd.Children.Add(bdD);
            fauilhbD.Click += new RoutedEventHandler(fauilhb_Click);
            fauilhbD.MouseEnter += new MouseEventHandler(fauilhbG_MouseEnter);
            fauilhbD.MouseLeave += new MouseEventHandler(fauilhbG_MouseLeave);

            var obj = selectFailure.FirstOrDefault(p => p.Item1.Id == item.Id);
            if (obj != null)
            {

                //bd.Background = new SolidColorBrush(Colors.Gray);  #E6EBF1 #E8EDF3
                bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                if (obj.Item2 == 0)
                {
                    bdD.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                    bdD.BorderBrush = new SolidColorBrush(Colors.Red);
                }
                else if (obj.Item2 == 1)
                {
                    bdZ.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                    bdZ.BorderBrush = new SolidColorBrush(Colors.Red);
                }
                else if (obj.Item2 == 2)
                {
                    bdG.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                    bdG.BorderBrush = new SolidColorBrush(Colors.Red);
                }
            }
            else bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#E6EBF1"));
        }

        void fauilhbG_MouseLeave(object sender, MouseEventArgs e)
        {
            var cuo = sender as HyperlinkButton;
            var bd = cuo.Parent as Border;
            if ((bd.BorderBrush as SolidColorBrush).Color != Colors.Red)
            {
                bd.Background = cuo.BorderBrush;
                bd.BorderBrush = cuo.BorderBrush;
            }
        }

        void fauilhbG_MouseEnter(object sender, MouseEventArgs e)
        {
            var cuo = sender as HyperlinkButton;
            var bd = cuo.Parent as Border;
            if ((bd.BorderBrush as SolidColorBrush).Color != Colors.Red)
            {
                //bd.BorderBrush = bd.Background = new SolidColorBrush(Colors.Gray);
                bd.BorderBrush = bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
            }
        }

        void fauilhb_Click(object sender, RoutedEventArgs e)
        {
            var cuo = sender as HyperlinkButton;
            var cuD = cuo.Tag as SimpleObject<Tbl_FailureInfo, int>;
            var bd = (cuo.Parent as Border).Tag as Border;

            var obj = selectFailure.FirstOrDefault(p => p.Item1.Id == cuD.Item1.Id);
            if (obj != null)
            {
                selectFailure.Remove(obj);
                if (obj.Item2 != cuD.Item2)
                    selectFailure.Add(cuD);
            }
            else
            {
                selectFailure.Add(cuD);
            }
            //tbFauilte.Text = selectFailure.Count > 0 ? string.Join(",", selectFailure.Select(p => p.Item1.FailureName)) : "";
            InitFauiltLevel(cuD.Item1, bd);
        }
        #endregion

        void hbPartName_Click(object sender, RoutedEventArgs e)
        {
            (this.Tag as PartSelectControl).gdParts.Visibility = System.Windows.Visibility.Visible;
            (this.Tag as PartSelectControl).cbEmpName.IsEnabled = (this.Tag as PartSelectControl).cbShopName.IsEnabled = (this.Tag as PartSelectControl).cbStationName.IsEnabled = true;
            this.Visibility = System.Windows.Visibility.Collapsed;
        }

        List<Tbl_DictInfo> FxDicts = new List<Tbl_DictInfo>();
        void sysClient_GetDictListByPidCompleted(object sender, SystemServiceReference.GetDictListByPidCompletedEventArgs e)
        {
            var re = e.Result.OrderBy(p => p.Sequence);
            if (e.UserState.ToString() == "8")
            {
                wpPositions.Children.Clear();
                foreach (var item in re)
                {
                    Border bd = new Border() { Tag = item };
                    if (selectPositionInfo != null && selectPositionInfo.Id == item.Id)
                    {
                        bd.BorderBrush = new SolidColorBrush(Colors.Red);
                        //bd.Background = new SolidColorBrush(Colors.Gray);
                        bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                    }
                    HyperlinkButton dichb = new HyperlinkButton();
                    dichb.Click += new RoutedEventHandler(dichb_Click);
                    TextBlock tb = new TextBlock()
                    {
                        Margin = new Thickness(5),
                        TextWrapping = TextWrapping.Wrap
                    };

                    tb.Text = SessionManager.IsCN_ZH ? item.DictName : item.en_DictName;

                    dichb.Content = tb;
                    bd.Child = dichb;
                    wpPositions.Children.Add(bd);
                }
            }
            else if (e.UserState.ToString() == "15")
            {
                FxDicts = e.Result.OrderBy(p => p.Sequence).ToList();

                #region 查询故障
                dataClient.GetFailureInfoListByPartNoCountAsync(CuPartInfo.PartNo, "", "");
                #endregion
            }
            //positionPageC.LoadPages(re.Count(), re.Count());
        }

        public Tbl_DictInfo selectPositionInfo;
        void ReLoadPositionStyle()
        {
            if (wpPositions.Children == null)
                return;
            foreach (var item in wpPositions.Children)
            {
                Border bd = item as Border;
                var cuD = bd.Tag as Tbl_DictInfo;
                if (selectPositionInfo != null && selectPositionInfo.Id == cuD.Id)
                {
                    bd.BorderBrush = new SolidColorBrush(Colors.Red);
                    //bd.Background = new SolidColorBrush(Colors.Gray);
                    bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#D0D7E3"));
                }
                else
                {
                    bd.BorderBrush = new SolidColorBrush(HelpTools.HtmlToColor("#BBBBBB"));
                    bd.Background = new SolidColorBrush(HelpTools.HtmlToColor("#E6EBF1"));
                }
            }
        }

        void dichb_Click(object sender, RoutedEventArgs e)
        {
            var cuo = sender as HyperlinkButton;
            var bd = cuo.Parent as Border;
            selectPositionInfo = bd.Tag as Tbl_DictInfo;// #E6EBF1          
            ReLoadPositionStyle();
            ReloadFauilte();
        }

        #region 查询保存故障
        void QuerySaveFailures()
        {
            var cuPart = (this.Tag as PartSelectControl);
            string cuPro = "";
            if (cuPart != null)
                cuPro = (cuPart.CuTbl_FailureRecord.ProductionNo == cuPart.selectFailureRecord.ProductionNo ? "" : cuPart.CuTbl_FailureRecord.ProductionNo);

            FailureServiceReference.FailureServiceClient faiClient = new FailureServiceClient();
            faiClient.GetFailureRecordListByConditionCountCompleted += new EventHandler<FailureServiceReference.GetFailureRecordListByConditionCountCompletedEventArgs>(faiClient_GetFailureRecordListByConditionCountCompleted);
            faiClient.GetFailureRecordListByConditionCountAsync("",
           CuPartInfo.PartNo, "", "", CuPartInfo.StationId, 0, null, null,
              -1, -1, DateTime.Now, DateTime.Now, cuPro, SessionManager.CurrentUser.Id);
            faiClient.CloseAsync();
        }

        void faiClient_GetFailureRecordListByConditionCountCompleted(object sender, FailureServiceReference.GetFailureRecordListByConditionCountCompletedEventArgs e)
        {
            fauiltRecordPageC.LoadPages(e.Result, 5);
        }
        void faiClient_GetFailureRecordListByConditionCompleted(object sender, FailureServiceReference.GetFailureRecordListByConditionCompletedEventArgs e)
        {
            var cure = e.Result.ToList();
            var cures = cure.Select(p => new SimpleObject<Tbl_FailureRecord, List<Tbl_DictInfo>>() { Item1 = p, Item2 = FxDicts });
            gdList.ItemsSource = cures;
        }

        void dpPagefauiltRecord_PageIndexChanged(object sender, EventArgs e)
        {
            var cuPart = (this.Tag as PartSelectControl);
            string cuPro = "";
            if (cuPart != null)
                cuPro = (cuPart.CuTbl_FailureRecord.ProductionNo == cuPart.selectFailureRecord.ProductionNo ? "" : cuPart.CuTbl_FailureRecord.ProductionNo);

            currentPage = fauiltRecordPageC.PageIndex + 1;
            FailureServiceReference.FailureServiceClient faiClient = new FailureServiceClient();
            faiClient.GetFailureRecordListByConditionCompleted += new EventHandler<FailureServiceReference.GetFailureRecordListByConditionCompletedEventArgs>(faiClient_GetFailureRecordListByConditionCompleted);
            faiClient.GetFailureRecordListByConditionAsync("",
              CuPartInfo.PartNo, "", "", CuPartInfo.StationId, 0, null, null,
              -1, -1, 5, currentPage, DateTime.Now, DateTime.Now, cuPro, SessionManager.CurrentUser.Id);
            faiClient.CloseAsync();
        }

        void faiClient_UpdateFailureRecordCompleted(object sender, UpdateFailureRecordCompletedEventArgs e)
        {
            if (e.Result)
            {
                (e.UserState as HyperlinkButton).IsEnabled = true;
                (e.UserState as HyperlinkButton).Content = MyStrings.bc;// "保存";
                QuerySaveFailures();

            }
        }
        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            var cuhb = sender as HyperlinkButton;
            cuhb.Content = MyStrings.bcz + "...";
            cuhb.IsEnabled = false;

            //var dgCurrentData = (cuhb.Tag as SimpleObject<Tbl_FailureRecord, List<Tbl_DictInfo>, string>);
            var dgCurrentData = (cuhb.Tag as SimpleObject<Tbl_FailureRecord, List<Tbl_DictInfo>>);
            faiClient.UpdateFailureRecordAsync(dgCurrentData.Item1, cuhb);
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            var cuhb = sender as HyperlinkButton;
            var cuo = (cuhb.Tag as Tbl_FailureRecord);
            var cuSp = (cuhb.Parent as StackPanel);
            ComboBox cb = cuSp.Children.FirstOrDefault(p => p is ComboBox) as ComboBox;
            cb.Visibility = System.Windows.Visibility.Visible;
            cuhb.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void cbFx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cuo = (sender as ComboBox);
            if (cuo.SelectedValue == null)
                return;
            var cuFr = (cuo.Tag as SimpleObject<Tbl_FailureRecord, List<Tbl_DictInfo>>);
            if (cuFr == null)
                return;
            if (cuo.SelectedValue.ToString() != "0")
                cuFr.Item1.RepairStatus = true;
            else cuFr.Item1.RepairStatus = false;
        }

        private void HyperlinkButton_Click_2(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show(MyStrings.qdyscm, MyStrings.ts, MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                return;
            var cuhb = sender as HyperlinkButton;
            var cuObjId = Int32.Parse(cuhb.Tag.ToString());
            faiClient.DeleteFailureRecordAsync(cuObjId);
        }

        void faiClient_DeleteFailureRecordCompleted(object sender, DeleteFailureRecordCompletedEventArgs e)
        {
            QuerySaveFailures();
        }
        #endregion

        //private void HyperlinkButton_Click_3(object sender, RoutedEventArgs e)
        //{
        //    var cuFr = (sender as HyperlinkButton).Tag as Tbl_FailureRecord;
        //    AddToTrakingWindow cuWin = new AddToTrakingWindow();
        //    cuWin.Tag = cuFr;
        //    cuWin.Show();
        //    cuWin.Closed += new EventHandler(cuWin_Closed);
        //}

        //void cuWin_Closed(object sender, EventArgs e)
        //{
        //    var cuWin = sender as AddToTrakingWindow;
        //    if (cuWin.DialogResult == false)
        //        return;
        //    if (cuWin.selectMemembers.Count < 1)
        //        return;
        //    var cuTrakingFR = cuWin.Tag as Tbl_FailureRecord;
        //    cuTrakingFR.TrackingFlag = true;
        //    Tbl_FailureTracking ft = new Tbl_FailureTracking()
        //    {
        //        FailureId = cuTrakingFR.FailureId,
        //        PartNo = cuTrakingFR.PartNo,
        //        PositionNo = cuTrakingFR.PositionNo,
        //        SourceNo = "1",
        //        Status = true,
        //        TeamId = cuWin.zzTdId,
        //        TrackingDate = DateTime.Now,
        //        ToEmpIds = string.Join(",", cuWin.selectMemembers)
        //    };
        //    faiClient.SaveFailureTrackingAsync(ft, cuTrakingFR.Id);
        //}

        //void faiClient_SaveFailureTrackingCompleted(object sender, SaveFailureTrackingCompletedEventArgs e)
        //{
        //    if (e.Result)
        //        QuerySaveFailures();
        //}
    }
}
